<?php

namespace App\Http\Controllers\Foundation;


use App\Model\Foundation\ScheduleType;
use Illuminate\Http\Request;
use App\Model\System\Token;
use Laravel\Lumen\Routing\Controller as BaseController;

use App\Http\Controllers\MessagePacket;


class ScheduleTypeController extends BaseController
{
    use MessagePacket;


    function  __construct(){}


    /**
     * 获取单一课程类型的信息
     * @param $id
     * @return packetMessage
     */
    function getOne($id)
    {
        try{
            $id = intval($id);
            $type = ScheduleType::find($id);
            if(empty($type)){
                return $this->fail(false, '数据不存在');
            }
            return $this->success($type);
        }catch (\Exception $e){
            return $this->fail(false, '获取失败：'.$e->getMessage());
        }

    }


    /**
     * 批量查询学生列表
     * @param Request $request
     * @return packetMessage
     */
    function getAll(Request $request)
    {
        try{
            $name = '';
            if($request->has('name')) $name= $request->get('name');
            $Type = ScheduleType::where('name', 'like', "%$name%");

            $types = $Type->get();


            if(empty($types)){
                return $this->fail(false, '数据不存在');
            }
            return $this->success($types);
        }catch (\Exception $e){
            return $this->fail(false, '获取失败：'.$e->getMessage());
        }

    }


    /**
     * 添加一个课程类型
     * @param Request $request
     * @return packetMessage
     */
    function  create(Request $request)
    {
        try{
            $courseType = new ScheduleType($request->all());
            $courseType->created_by = Token::user()->userid;
            $courseType->updated_by = Token::user()->userid;
            $courseType->save();
            return $this->success($courseType,"创建成功");
        }catch (\Exception $e){
            return $this->fail(false,'创建失败:'.$e->getMessage());
        }

    }


    /**
     * 更新某个课程类型的信息
     * @param Request $request
     * @return packetMessage
     */
    function  update($id, Request $request)
    {
        try{
            if(empty($id)){
                if($request->has('id'))
                    $id = $request->get('id');
            }
            $courseType = ScheduleType::find($id);
            $courseType->update($request->all());
            $courseType->updated_by = Token::user()->userid;
            $courseType->save();
            return $this->success($courseType,"修改成功");
        }catch (\Exception $e){
            return $this->fail(false,'更新失败：'.$e->getMessage());
        }
    }


    /**
     * 删除某个学员
     * @param $id
     * @return packetMessage
     */
    function  delete($id)
    {
        try{
            ScheduleType::destroy($id);
            return $this->success(true);
        }catch (\Exception $e){
            return $this->fail(false,'删除失败：'.$e->getMessage());
        }
    }

}
